#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import Conf

class Db:
    __cursor = None
    __db = None
    
    def __init__(self):
        self.connect()
    
    def connect(self):
        self.__db = MySQLdb.connect(db=Conf.dbdatabase, host=Conf.dbhost, user=Conf.dbuser, passwd=Conf.dbpass,charset=Conf.dbcharset, use_unicode=Conf.dbuse_unicode)
        self.__cursor = self.__db.cursor()
        
    def query(self,query,params = None):
        if self.__cursor == None:
            self.connect()
        try:
            if params == None:
                self.__cursor.execute(query)
                return 0
            else:
                self.__cursor.execute(query, params)
                return 0
        except MySQLdb.Error,e:
            #print e
            return e.args[0] 

        
    def fetchall(self):
        if self.__cursor != None:
            return self.__cursor.fetchall()
    
    def escape(self, str):
        return self.__db.escape_string(str)
    
    def errCode(self):
        return self.__db.show_warnings()
